package com.bsierp.util.tags;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.bsierp.business.pojo.ProductParameter;
import com.bsierp.util.hibernate.HibernateSessionFactory;

public class DisplayProductParameterHtmlCheckBox extends TagSupport {
	String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private Log log = LogFactory.getLog(this.getClass().getName());

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		String hql = "from ProductParameter p";
		try {
			HttpSession ses = pageContext.getSession();
			SessionFactory sessionFactory = HibernateSessionFactory
					.getSessionFactory();
			Session session = sessionFactory.openSession();
			Transaction tx = null;
			String checkbox_str = "";
			try {
				tx = session.beginTransaction();
				Query q = session.createQuery(hql);
				List l = q.list();
				for (int i = 0; i < l.size(); i++) {
					ProductParameter pp = (ProductParameter) l.get(i);
					checkbox_str=checkbox_str+"<input type=checkbox value="+pp.getProductparameterId()+">"+pp.getProductparameterName()+"";
				}
			} catch (Exception e) {
				e.printStackTrace();

			} finally {
				session.close();
			}

			out.println("<form action=\"\" id=\"" + this.getId()+ "\">");
			out.println(checkbox_str);
			out.println("</form>");

		} catch (Exception ex) {
			throw new JspTagException("IOException:" + ex.toString());
		}
		return super.doEndTag();
	}
}
